<?php
require '../../config.php';
$query="SELECT * from rutas";
$resultset = mysql_query($query,$conexion);
if(isset( $_REQUEST['activo']))
	$activo = $_REQUEST['activo'];
else 
	$activo=0;


if(!$resultset)
{
	mysql_close();
	echo json_encode('error en el query: ' . mysql_error());
}
elseif(!mysql_num_rows($resultset))
{
	mysql_close();
	echo "<button type='button' class='btn btn-info'   onclick='nuevaruta()'>Primero agregue una ruta&nbsp<span class= 'glyphicon glyphicon-plus'></span></button>";
}
else
{

	
	echo   "<br><table class='table table-bordered'>";
	echo   "<tr class='danger'>";
	echo   "<th>RUTAS</th><th>PLANEAMIENTO</th><th>OPCIONES</th>";
	echo   "</tr>";
	echo   "<tbody>";
	echo   "<tr >";
	
		//PRIMERA TABLA
		echo   "<td>";
				echo   "<table class='table table-bordered'>";
				echo   "<tr class='success'>";
				echo   "<th>Id ruta</th><th>No. Ruta</th><th>Nombre</th><th>Descripcion</th><th>Opciones</th>";
				echo   "</tr>";
				echo   "<tbody>";
				$aux=0;
				while($row = mysql_fetch_assoc($resultset))
				{
					if($aux==0){
						$color="#FFFFFF";
						$aux=1;
					}else{
						$color="#F9F9F9";
						$aux=0;
					}
					$idruta = $row['idrutas'];
					if($activo==$idruta)
						echo ("<tr class='warning'>");
					else
						echo ("<tr bgcolor='".$color."'>");
					echo 	"<td><button type='button' class='btn btn-link btn-xs'  onclick='verplaneacion(".$row['idrutas'].")'>".$row['idrutas']."</button></td>";
					echo   "<td>".$row['numeroruta']."</td>";
					echo   "<td>"._convert($row['nombre'])."</td>";
					echo   "<td>"._convert($row['descripcion'])."</td>";
					echo   "<td><button type='button' class='btn btn-link btn-xs' onclick='verclientesrutas($row[idrutas])'> Ver Clientes de esta ruta <span class='glyphicon glyphicon-list'></span> </button></td>";
					echo   "</tr>";
				}
				echo   "</tbody>";
				echo   "</table>";	
		echo   "</td>";
		
		echo   "<td>";
			if($activo != 0)
			{
			
				echo   "<table class='table table-bordered'>";
				echo   "<tr class='success'>";
				echo   "<th>Id Plan</th><th>Fecha</th><th>Estado</th><th>Opcion</th>";
				echo   "</tr>";
				echo   "<tbody>";
				if($activo > 0)
				{
					$querys= "SELECT idplanes,fecha,estado from planes where idruta = '".$activo."' order by idplanes desc";
					$resultset2 = mysql_query($querys,$conexion);
					if(mysql_num_rows($resultset2)>0)
					{
				
						$aux=0;
						while($row2 = mysql_fetch_assoc($resultset2))
						{
							if($aux==0){
								$color="#FFFFFF";
								$aux=1;
							}else{
								$color="#F9F9F9";
								$aux=0;
							}
							if($row2['estado']!= 2)
								echo   "<tr bgcolor='".$color."'>";
							else 
								echo   "<tr class='danger'>";
							echo 	"<td>".$row2[idplanes]."</td>";
							echo   "<td>".date("d M Y",strtotime($row2['fecha']))."</td>";
							echo   "<td>".icono($row2['estado'])."</td>";
							if($row2['estado']!= 2)
								echo   "<td><a  href='#' <span class='glyphicon glyphicon-remove' onclick='quitar2(".$row2[idplanes].")'> Quitar</span></a></td>";
							else 
								echo   "<td><a  href='#' <span class='glyphicon glyphicon-remove' onclick='quitar2(".$row2[idplanes].")'>Quitar</span></a>&nbsp&nbsp<a  href='#' <span class='glyphicon glyphicon-refresh' onclick='reprogramar(".$row2[idplanes].")'>Reprogramar</span></a></td>";
							
							echo   "</tr>";
						}
				
					}
						
				}
				echo   "</tbody>";
				echo   "</table>";
			}
		echo   "</td>";
		echo   "<td>";
		
		// TERCERA TABLA
			echo   "<table class='table table-bordered'>";
			echo   "<tbody>";
			echo   "<tr><th><button type='button' class='btn btn-info'   onclick='programarRuta(".$activo.")'>Planificar esta ruta&nbsp<span class= 'glyphicon glyphicon-list-alt'></span></button></th></tr>";
			echo 	"<tr><th><button type='button' class='btn btn-info'   onclick='nuevaruta()'>Crear ruta&nbsp<span class= 'glyphicon glyphicon-plus'></span></button>";
			
			echo   "</tbody>";
			echo   "</table>";
		echo   "</td>";
		
	echo   "</tr>";
	echo   "</tbody>";
	echo   "</table>";
	mysql_close();

}

function icono($estado)
{
	$iconos="";
	if($estado == 0)
		$iconos = "<span class= 'glyphicon glyphicon-off'></span>";
	if($estado == 1)
		$iconos = "<span class= 'glyphicon glyphicon-ok-sign'></span>";
	if($estado == 2)
		$iconos = "<span class= 'glyphicon glyphicon-exclamation-sign'></span>";
	return $iconos;
}

function _convert($content) {
	if(!mb_check_encoding($content, 'UTF-8')
	OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

		$content = mb_convert_encoding($content, 'UTF-8');

		if (mb_check_encoding($content, 'UTF-8')) {
			// log('Converted to UTF-8');
		} else {
			// log('Could not converted to UTF-8');
		}
	}
	return $content;
}

?>